package com.wans.guli.order.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wans.guli.order.vo.MemberVO;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class OrderUserLoginCheckInterceptor implements HandlerInterceptor {

    public static final ThreadLocal<MemberVO> memberInfoThreadLocal = new ThreadLocal<>();

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 方法执行之后，返回modelAndView之前
     *
     * @param request  current HTTP request
     * @param response current HTTP response
     * @param handler  handler  that started asynchronous
     *                 execution, for type and/or instance examination
     * @param ex       exception thrown on handler execution, if any
     * @throws Exception in case of errors
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    /**
     * 方法执行之后，返回modelAndView之前
     *
     * @param request      current HTTP request
     * @param response     current HTTP response
     * @param handler      handler  that started asynchronous
     *                     execution, for type and/or instance examination
     * @param modelAndView the {@code ModelAndView} that the handler returned
     *                     (can also be {@code null})
     * @throws Exception in case of errors
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 清除线程变量
        memberInfoThreadLocal.remove();

    }

    /**
     * 方法执行之前
     *
     * @param request  current HTTP request
     * @param response current HTTP response
     * @param handler  chosen handler to execute, for type and/or instance evaluation
     * @return {@code true} if the execution chain should proceed with the
     * next interceptor or the handler itself. Else, DispatcherServlet assumes
     * that this interceptor has already dealt with the response itself.
     * @throws Exception in case of errors
     */
    @Override
    @SneakyThrows
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        // 获取用户信息 - session - redis
        String loginMember = (String) request.getSession().getAttribute("loginMember");
        if (loginMember != null) {
            // 这次请求用户是登录的
            MemberVO member = objectMapper.readValue(loginMember, MemberVO.class);
            if (member != null) {
                // 放入线程变量
                memberInfoThreadLocal.set(member);
                // 用户登录
                return true;
            }
        }

        // 用户未登录
        response.sendRedirect("http://auth.gulimall.com/login.html");
        return false;
    }
}
